#import "QRCodeScannerView.h"

@interface QRCodeScannerView ()

/** 扫描框区域 */
@property (nonatomic, assign) CGRect scanFrame;

/** 扫描线 */
@property (nonatomic, strong) UIImageView *scanLineImageView;

/** 扫描动画 */
@property (nonatomic, strong) CABasicAnimation *scanAnimation;

@end

@implementation QRCodeScannerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    // 设置背景色为半透明黑色
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    
    // 计算扫描框大小（正方形，宽高为屏幕宽度的70%）
    CGFloat scanWidth = MIN(self.bounds.size.width, self.bounds.size.height) * 0.7;
    CGFloat scanX = (self.bounds.size.width - scanWidth) / 2;
    CGFloat scanY = (self.bounds.size.height - scanWidth) / 2;
    self.scanFrame = CGRectMake(scanX, scanY, scanWidth, scanWidth);
    
    // 绘制扫描框和周围遮罩
    [self setNeedsDisplay];
    
    // 添加扫描线
    [self addScanLine];
}

- (void)addScanLine {
    self.scanLineImageView = [[UIImageView alloc] init];
    // 可以使用自己的扫描线图片，这里用颜色创建一个简单的线条
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.scanFrame.size.width, 2), NO, 0);
    [[UIColor greenColor] setFill];
    UIRectFill(CGRectMake(0, 0, self.scanFrame.size.width, 2));
    self.scanLineImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.scanLineImageView.frame = CGRectMake(self.scanFrame.origin.x, self.scanFrame.origin.y,
                                              self.scanFrame.size.width, 2);
    [self addSubview:self.scanLineImageView];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    // 绘制遮罩层（扫描框外的半透明区域）
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor colorWithWhite:0 alpha:0.4] setFill];
    CGContextFillRect(context, rect);
    
    // 清除扫描框区域（形成透明窗口）
    CGContextClearRect(context, self.scanFrame);
    
    // 绘制扫描框边框
    UIColor *borderColor = [UIColor whiteColor];
    [borderColor setStroke];
    CGContextSetLineWidth(context, 1);
    
    // 绘制矩形边框
    CGContextStrokeRect(context, self.scanFrame);
    
    // 绘制四个角
    CGFloat cornerLength = 20;
    CGFloat cornerWidth = 3;
    [UIColor.greenColor setStroke];
    CGContextSetLineWidth(context, cornerWidth);
    
    // 左上角
    CGContextMoveToPoint(context, self.scanFrame.origin.x, self.scanFrame.origin.y + cornerLength);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x, self.scanFrame.origin.y);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x + cornerLength, self.scanFrame.origin.y);
    CGContextStrokePath(context);
    
    // 右上角
    CGContextMoveToPoint(context, self.scanFrame.origin.x + self.scanFrame.size.width - cornerLength, self.scanFrame.origin.y);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x + self.scanFrame.size.width, self.scanFrame.origin.y);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x + self.scanFrame.size.width, self.scanFrame.origin.y + cornerLength);
    CGContextStrokePath(context);
    
    // 左下角
    CGContextMoveToPoint(context, self.scanFrame.origin.x, self.scanFrame.origin.y + self.scanFrame.size.height - cornerLength);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x, self.scanFrame.origin.y + self.scanFrame.size.height);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x + cornerLength, self.scanFrame.origin.y + self.scanFrame.size.height);
    CGContextStrokePath(context);
    
    // 右下角
    CGContextMoveToPoint(context, self.scanFrame.origin.x + self.scanFrame.size.width - cornerLength, self.scanFrame.origin.y + self.scanFrame.size.height);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x + self.scanFrame.size.width, self.scanFrame.origin.y + self.scanFrame.size.height);
    CGContextAddLineToPoint(context, self.scanFrame.origin.x + self.scanFrame.size.width, self.scanFrame.origin.y + self.scanFrame.size.height - cornerLength);
    CGContextStrokePath(context);
}

- (CABasicAnimation *)scanAnimation {
    if (!_scanAnimation) {
        _scanAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
        _scanAnimation.fromValue = @(self.scanFrame.origin.y);
        _scanAnimation.toValue = @(self.scanFrame.origin.y + self.scanFrame.size.height);
        _scanAnimation.duration = 2.0;
        _scanAnimation.repeatCount = MAXFLOAT;
        _scanAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    }
    return _scanAnimation;
}

- (void)startScanAnimation {
    [self.scanLineImageView.layer addAnimation:self.scanAnimation forKey:@"scanAnimation"];
}

- (void)stopScanAnimation {
    [self.scanLineImageView.layer removeAnimationForKey:@"scanAnimation"];
}

- (void)resetScanAnimation {
    [self stopScanAnimation];
    self.scanLineImageView.center = CGPointMake(CGRectGetMidX(self.scanFrame), self.scanFrame.origin.y);
    [self startScanAnimation];
}

@end

